# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=wxPython
_wx_basever=3.0
pkgbase=mingw-w64-${_realname}4.0
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}4.0"
pkgver=4.0.7.2
_pkgver="${pkgver%.*}"
_post="${pkgver##*.}"
pkgrel=4
pkgdesc="A wxWidgets GUI toolkit for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("spdx:wxWindows")
url="https://www.wxpython.org/"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-python-numpy" )
         "${MINGW_PACKAGE_PREFIX}-python-pillow"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python-requests"
             "${MINGW_PACKAGE_PREFIX}-sip4"
             "${MINGW_PACKAGE_PREFIX}-waf"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'patch'
             # for waf
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('strip' 'staticlibs' 'buildflags')
source=("https://files.pythonhosted.org/packages/source/w/wxPython/wxPython-$_pkgver.post$_post.tar.gz"
        "0001-wscript-wxWidgets-win-from-cygwin.patch"
        "0002-etg-handle-cast.patch"
        "0003-Optionally-use-MinGW-toolchain.patch"
        "0007-etg-Add-some-missing-private-assignment-operators.patch"
        "0100-etgtools-wxHandleFatalExceptions.patch"
        "0201-wxWidgets.Doxyfile.patch"
        "0202-wxWidgets-Fix-quoting-in-Doxyfile-ALIASES.patch"
        "0203-wxWidgets-Fix-c-11-narrowing-error-when-using-clang-on-Windows.patch"
        "0204-wxWidgets-3.0-clang-windows-link.patch"
        "0205-wxWidgets-3.0-gdiplus-math-min-max.patch")
noextract=("${_realname}-$_pkgver.post$_post.tar.gz")
sha256sums=('5a229e695b64f9864d30a5315e0c1e4ff5e02effede0a07f16e8d856737a0c4e'
            '88cc6c7c9cdb6dd6f8e7a74356b275262f4e8d920d64bae987408ee2ff651bc1'
            '7af6ee0a93cfd5d5a621ed09ba91a42a017c8c42a2ff4936b98bdf4014086359'
            'dddbf050b84e9bb330cb56f61181555d4e0f083ee68736e0f5b298d910daa0c5'
            'a1dc197b70dbe72b9b8a5298391cc5b1fdc77b295461e8e3d313a0c92096fc39'
            'f2416985340363453c44b62c659d378e345a1e1b28c2173bbad2671a751cf844'
            '5d9c49bde4f60c1a818fb1b25ade2163467349fbf2e074d5fef2058748e88965'
            'fda9a066a29d85035405da02f5fa616c567b95c45b36c52a377f0014c790beb2'
            '5d224c4fdb2a2162b43e49789ec1169d05c12d25cf6f6f6b45fe13193a5966b3'
            'b8b49b1df4a7c53d72c7c117606ac9dc44589da474ca58f0806b107469f33dcb'
            '4915d09f217270956cad3de7cf0e3fdaec4847cfda46d4b568bde26a8bd5f94a')

prepare() {
  plain "Extracting ${_realname}-$_pkgver.post$_post.tar.gz due to symlink(s) without pre-existing target(s)"
  cd "${srcdir}"
  [[ -d ${_realname}-$_pkgver.post$_post ]] && rm -rf ${_realname}-$_pkgver.post$_post
  tar zxf "${srcdir}/${_realname}-$_pkgver.post$_post.tar.gz" || true

  cd "${srcdir}/${_realname}-$_pkgver.post$_post"
  rm -f etg/{_,}webkit.py sip/gen/{_,}webkit.sip
  patch -Np1 -i "${srcdir}/0001-wscript-wxWidgets-win-from-cygwin.patch"
  # clang doesn't like casting from pointer to smaller integer type (long)
  # https://github.com/wxWidgets/Phoenix/pull/1972
  patch -Np1 -i "${srcdir}/0002-etg-handle-cast.patch"
  patch -Np1 -i "${srcdir}/0003-Optionally-use-MinGW-toolchain.patch"
  patch -Np1 -i "${srcdir}/0007-etg-Add-some-missing-private-assignment-operators.patch"
  # https://github.com/wxWidgets/Phoenix/commit/3500ac7a9e7377c154a507dd7ea1b5b7bfda8c09
  patch -Np1 -i "${srcdir}/0100-etgtools-wxHandleFatalExceptions.patch"

# When wxWidgets 3.2 is released the below command will likely be needed
#  sed -i "s|WX_CONFIG = 'wx-config'|WX_CONFIG = 'wx-config-3.0'|" build.py

  cd ext/wxWidgets
  patch -Np1 -i "${srcdir}/0201-wxWidgets.Doxyfile.patch"
  patch -Np1 -i "${srcdir}/0202-wxWidgets-Fix-quoting-in-Doxyfile-ALIASES.patch"
  patch -Np1 -i "${srcdir}/0203-wxWidgets-Fix-c-11-narrowing-error-when-using-clang-on-Windows.patch"
  patch -Np1 -i "${srcdir}/0204-wxWidgets-3.0-clang-windows-link.patch"
  patch -Np1 -i "${srcdir}/0205-wxWidgets-3.0-gdiplus-math-min-max.patch"
  cd ../..
}

build() {
  cd "${srcdir}/${_realname}-$_pkgver.post$_post"

  export PYTHONDONTWRITEBYTECODE=1
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  WAF="{$MINGW_PREFIX}/bin/waf" \
  SIP="${MINGW_PREFIX}/bin/sip" \
  DOXYGEN="${MINGW_PREFIX}/bin/doxygen" \
  WX_CONFIG="${MINGW_PREFIX}/bin/wx-config-${_wx_basever}" \
    "${MINGW_PREFIX}/bin/python" build.py \
      --prefix="${MINGW_PREFIX}" --python="${MINGW_PREFIX}/bin/python.exe" \
      --release --use_syswx --nodoc --cairo --verbose \
      --no_allmo --no_magic --regenerate_sysconfig \
        dox touch etg sip

  # The build errors out with 1 or 2 jobs
  local _jobs=${MAKEFLAGS:--j3}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  CC_NAME=${CC} CC="${MINGW_PREFIX}/bin/${CC}.exe" \
  CXX_NAME=${CXX} CXX="${MINGW_PREFIX}/bin/${CXX}.exe" \
  LDFLAGS="${LDFLAGS} $("${MINGW_PREFIX}/bin/python-config" --ldflags)" \
  PYTHON_CONFIG="${MINGW_PREFIX}/bin/python-config" \
    "${MINGW_PREFIX}/bin/waf" \
      --prefix="${MINGW_PREFIX}" --python="${MINGW_PREFIX}/bin/python.exe" \
      --no_msvc --check-c-compiler=${CC} --check-cxx-compiler=${CXX} \
      --color=yes --jobs=${_jobs#-j} \
      --wx_config="sh.exe ${MINGW_PREFIX}/bin/wx-config-${_wx_basever}" \
      --no_magic --nopyc --nopyo --nopycache \
        configure build
}

package() {
  cd "${srcdir}/${_realname}-$_pkgver.post$_post"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    "${MINGW_PREFIX}/bin/python" setup.py install \
      --prefix="${MINGW_PREFIX}" --root="${pkgdir}" --skip-build

  # remove shebang line
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done

  install -vDm 644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/LICENSE.txt"
}
